package org.geekbang.thinking.in.spring.simple;

import lombok.extern.slf4j.Slf4j;
import org.geekbang.thinking.in.spring.simple.domain.DemoBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: chenzhongyong
 * Date: 2021/7/1
 * Time: 19:11
 */
public class SimpleBeanLifecycleDemo {


    public static void main(String[] args) {

        DemoBean demoBean = null;

        String configPath = "application-context.xml";

        //1
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(configPath);
        demoBean = (DemoBean)classPathXmlApplicationContext.getBean("demoBean");

//        classPathXmlApplicationContext.refresh();

        //2
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

        int beanNumbers = beanDefinitionReader.loadBeanDefinitions(configPath);
        System.out.println("已加载 BeanDefinition 数量：" + beanNumbers);
        demoBean = (DemoBean) beanFactory.getBean("demoBean");

        System.out.println(demoBean);


    }
}
